﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using MCSFramework.Common;
using MCSFramework.IFStrategy;

namespace MCSFramework.BLL
{
    public abstract class BaseSimpleBll<T> : ISimpleBll<T>
    {
        protected T _m = default(T);

        private int _keytype = 1;//标志主键类型 1:int   2:Guid   3:string;分别对应_id,_guid,_code三个私有成员变量

        private int _id;
        private Guid _guid;
        private string _code;

        private ISimpleDAL<T> _d;
        protected ISimpleDAL<T> _DAL
        {
            get { return _d; }
        }

        #region 构造方法

        public BaseSimpleBll(string fullclassname)
        {
            _d = (ISimpleDAL<T>)DataAccess.CreateObject(fullclassname);
        }

        public BaseSimpleBll(string fullclassname, bool dalbycache)
        {
            _d = (ISimpleDAL<T>)DataAccess.CreateObject(fullclassname, dalbycache);
        }
        #endregion

        #region 针对三种主键类型（int，guid，string）的FillModel方法,用于创建类实例

        protected void FillModel(int id)
        {
            FillModel(id, false);
        }

        /// <summary>
        /// 根据bycache决定是否从内存中获取对象
        /// </summary>
        /// <param name="id"></param>
        /// <param name="bycache"></param>
        protected void FillModel(int id, bool bycache)
        {
            _keytype = 1;
            _id = id;

            if (_d == null)
            {
                throw new Exception("数据库访问对象的DAL的classname不能为空");
            }
            if (bycache)
            {
                string bllname = this.ToString();
                if (bllname.LastIndexOf('.') > 0)
                {
                    bllname = bllname.Substring(0, bllname.LastIndexOf('.') + 1);
                }
                string cachekey = "Model-" + bllname + "-" + id.ToString();
                _m = (T)MCSFramework.Common.DataCache.GetCache(cachekey);
                if (_m == null)
                {
                    _m = _d.GetModel(id);
                    if (_m != null)
                    {
                        int ModelCache = MCSFramework.Common.ConfigHelper.GetConfigInt("ModelCache");
                        //System.Web.Caching需添加System.Web的引用
                        MCSFramework.Common.DataCache.SetCache(cachekey, _m, DateTime.Now.AddMinutes(ModelCache), System.Web.Caching.Cache.NoSlidingExpiration);
                    }
                }
            }
            else
            {
                _m = _d.GetModel(id);
            }
        }

        protected void FillModel(Guid id)
        {
            FillModel(id, false);
        }

        protected void FillModel(Guid id, bool bycache)
        {
            _keytype = 2;
            _guid = id;

            if (_d == null)
            {
                throw new Exception("数据库访问对象的DAL的classname不能为空");
            }
            if (bycache)
            {
                string bllname = this.ToString();
                if (bllname.LastIndexOf('.') > 0)
                {
                    bllname = bllname.Substring(bllname.LastIndexOf('.') + 1);
                }
                string cachekey = "Model-" + bllname + "-" + id.ToString();
                _m = (T)MCSFramework.Common.DataCache.GetCache(cachekey);
                if (_m == null)
                {
                    _m = _d.GetModel(id);
                    if (_m != null)
                    {
                        int ModelCache = ConfigHelper.GetConfigInt("ModelCache");
                        DataCache.SetCache(cachekey, _m, DateTime.Now.AddMinutes(ModelCache), System.Web.Caching.Cache.NoSlidingExpiration);
                    }
                }
            }
            else
            {
                _m = (T)_d.GetModel(id);
            }
        }

        protected void FillModel(string code)
        {
            FillModel(code, false);
        }

        protected void FillModel(string code, bool bycache)
        {
            _keytype = 3;
            _code = code;
            if (_d == null)
            {
                throw new Exception("数据库访问对象的DAL的classname不能为空");
            }
            if (bycache)
            {
                string bllname = this.ToString();
                if (bllname.LastIndexOf('.') > 0)
                {
                    bllname = bllname.Substring(bllname.LastIndexOf(',') + 1);
                }
                string cachekey = "Model-" + bllname + "-" + code.ToString();
                _m = (T)DataCache.GetCache(cachekey);
                if (_m == null)
                {
                    _m = _d.GetModel(code);
                    if (_m != null)
                    {
                        int ModelCache = ConfigHelper.GetConfigInt("ModelCache");
                        DataCache.SetCache(cachekey, _m, DateTime.Now.AddMinutes(ModelCache), System.Web.Caching.Cache.NoSlidingExpiration);
                    }
                }
            }
            else
            {
                _m = _d.GetModel(code);
            }
        }
        #endregion

        #region ISimpleBll<T> 成员

        public T Model
        {
            get
            {
                return _m;
            }
            set
            {
                _m = value;
            }
        }

        public int Add()
        {
            return _d.Add(_m);
        }

        public int Update()
        {
            return _d.Update(_m);
        }

        public int Delete()
        {
            if (_keytype==1)
            {
                return _d.Delete(_id);
            }
            else if (_keytype == 2)
            {
                return _d.Delete(_guid);
            }
            else
            {
                throw new Exception("删除时，关键字不能为字符串");
            }
        }

        public int Delete(int id)
        {
            return _d.Delete(id);
        }

        public int Delete(Guid id)
        {
            return _d.Delete(id);
        }

        public bool Exists(int id)
        {
            return _d.Exists(id);
        }

        public bool Exists(Guid id)
        {
            return _d.Exists(id);
        }

        #endregion


        /// <summary>
        /// 根据自定义SQL条件获取Model列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public virtual IList<T> _GetModelList(string condition)
        {
            return _d.GetModelList(condition);
        }
    }
}
